package com.csaba.connector.axa;

import java.io.StringReader;
import java.util.logging.Level;
import java.util.logging.Logger;

import net.htmlparser.jericho.Element;
import net.htmlparser.jericho.Source;
import net.htmlparser.jericho.TextExtractor;

import org.json.JSONException;
import org.json.JSONObject;

import com.csaba.connector.I18NServiceException;
import com.csaba.connector.ServiceException;
import com.csaba.connector.axa.model.AXABank;
import com.csaba.connector.model.AbstractRemoteObject;
import com.csaba.util.LoggerHelper;

public class AXAUtil
{
	private final static Logger logger = LoggerHelper.getDefaultLogger();

	private AXAUtil()
	{
		// do not instantiate
	}

	public static void setIntProperty( final AbstractRemoteObject remote, final JSONObject json, final String property )
	{
		if ( json.has(property) && !json.isNull(property) )
		{
			try
			{
				final int value = json.getInt(property);
				remote.setRemoteProperty(property, value);
			}
			catch ( final Exception e )
			{
				logger.log(Level.SEVERE, "Failed to set string property: " + property, e);
			}
		}
	}

	public static void setStringProperty( final AbstractRemoteObject remote, final JSONObject json,
			final String property )
	{
		if ( json.has(property) && !json.isNull(property) )
		{
			try
			{
				final String value = json.getString(property).trim();
				if ( value.length() > 0 )
					remote.setRemoteProperty(property, value);
			}
			catch ( final Exception e )
			{
				logger.log(Level.SEVERE, "Failed to set string property: " + property, e);
			}
		}
	}

	public static ServiceException serverError( final String json )
	{
		/*{
		"Exception":"Munkameneti hiba",
		"Message":"Biztons\u00E1gi okokb\u00F3l az \u00D6n kapcsol\u00F3d\u00E1si ideje lej\u00E1rt! K\u00E9rj\u00FCk, jelentkezzen be \u00FAjra!",
		"ExceptionType":"SESSION_EXCEPTION",
		"ExceptionCode":"SESSION_CODE_SESSION_EXPIRED"
		 */
		try
		{
			logger.severe("Server error: " + json);
			final JSONObject error = new JSONObject(json);
			final String message = error.getString("Message");
			return new I18NServiceException(AXABank.getInstance(), "err.serverError", message);
		}
		catch ( final JSONException e )
		{
			logger.log(Level.SEVERE, "Failed to parse server side error message.", e);
			return new I18NServiceException(AXABank.getInstance(), "err.fatalServerError", e);
		}

	}

	public static void checkForHTMLError( final String html, final String errorMessage ) throws ServiceException
	{
		//verify result
		//		<div class="error"><div class="inner">
		//		A felhasználói név vagy a jelszó nem helyes. Kérjük, próbálja meg újra vagy hívja Telefonos Ügyfélszolgálatunkat a (06 1) 465 65 65-ös telefonszámon!
		//		</div></div>

		//		<div class="error"><div class="inner">
		//		Hibás jóváhagyó kód. Kérjük, jelentkezzen be újra!
		//		</div></div>

		final int pos = html.indexOf("class=\"error\"");
		if ( pos > 0 )
		{
			String message = "-";
			try
			{
				final Source source = new Source(new StringReader(html));

				// find the account table
				final Element messageTag = source.getFirstElementByClass("error");
				message = new TextExtractor(messageTag).toString();
			}
			catch ( final Exception e )
			{
				logger.log(Level.SEVERE, "Failed to parse HTML error message.", e);
			}
			throw new I18NServiceException(AXABank.getInstance(), errorMessage, message);
		}
	}

}
